import {GptVersion} from "@/app/constants";
import {MessageRole} from "@/type/chat";
import {useAccessStore} from "@/app/store/access";


const host = "http://localhost:18080";



function getHeaders() {
    const accessState = useAccessStore.getState()

    const headers = {
        Authorization: accessState.token,
        'Content-Type': 'application/json;charset=utf-8'
    }

    return headers
}

/**
 * 从本地获取角色列表
 */
export const getRoleList = () => {

    // 从本地 json 文件获取
    return fetch(`/prompts.json`).then((res) =>
        res.json()
    );
}

/**
 * 流式应答接口
 * @param data
 */
export const completions = (data: {
    messages: { content: string; role: MessageRole }[],
    model: GptVersion
}) => {
    return fetch(`${host}/api/v1/chatgpt/chat/completions`, {
        method: 'post',
        headers: getHeaders(),
        body: JSON.stringify(data)
    });
};


export const login=(token:string)=>{
    const accessState=useAccessStore.getState();
    return fetch(`${host}/api/v1/auth/login`,{
        method:'post',
        headers:{
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body:`code=${accessState.accessCode}`
    })
}